<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Ultimate_posts_module extends MY_Module {
	
	public function __construct() {
		parent::__construct();
		$this->load->model('term_m','term');
		$this->load->model('post_m','post');
	}
	
	public function admin($config) {
		$config['config']['list_cate'] = $this->term->select('term_id, term_name,term_parent')->get_menu_array(0, 'category', '-');
		if (isset($config['config']['cateid']) && $config['config']['cateid'])
			$config['config']['cateid'] = explode(',', $config['config']['cateid']);
		$this->view('admin', $config['config']);
	}
	
	public function module_update($data) {
		if (isset($data['cateid']) && $data['cateid']) {
			$data['cateid'] = implode(',', $data['cateid']);
		}else {
			$data['cateid']  = '';
		}
		return $data;
	}
	/**
	 * 
	 * @param object $config
	 * 
	 */
	public function run($config) {
		$config_default = array(
			'view' => 'view_1',
			'limit' => 5,
			'title_link' => '',
			'order_by' => 'post_time_start',
			'order' => 'DESC',
			'cateid' => '0',
		);	
		$config = array_merge($config_default,$config);

		if($config['limit'] > 50) $config['limit'] = 50;
		$config['posts'] = array();
		$params = array(
			'posts_per_page'   => @$config["limit"],
			'orderby'          => @$config['order_by'],
			'order'            => @$config['order'],
			'include'          => @$config['post_id'],
			'exclude'          => @$config['ignore_post_id']
		);

		if(isset($config['post_level']) && $config['post_level'] != '' && $config['post_level'] != '0'){
			$params['meta_key'] = '_cms_post_level';
			$params['meta_value'] = $config['post_level'];
		}

		$params['category'] = (isset($config['cateid'] ) && $config['cateid'])?explode(',', $config['cateid']) : '';
		
		if(isset($config['iscategory']) && $config['iscategory'] == 'true'){
			
			if(@$this->msystem->get_global('cat_id') && $this->msystem->get_global('cat_id') != '') {
				$params['category'][] = @$this->msystem->get_global('cat_id');
				$cate_sub = $this->category_m->get_menu_slug_array($this->msystem->get_global('cat_id'));
				foreach ($cate_sub as $key => $value) {
					$params['category'][] = $key;
				}
			}
		}
		if(in_array(@$config['view'], array('view_9','view_16'))){
			//FOR PAGINATION
			$config['use_page_numbers'] = TRUE;
			$config['prefix'] = 'trang-';
			$config['anchor_class'] = '';
			$config['num_links'] = 10;
			$config['next_link'] = FALSE;
			$config['first_link'] = FALSE;
			$config['last_link'] = FALSE;
			$config['prev_link'] = FALSE;
			$config['cur_tag_open'] = '<li class="active"><a href="#">';
			$config['cur_tag_close'] = '</a></li>';
			$config['first_tag_open'] = '<li>';
			$config['first_tag_close'] = '</li>';
			$config['last_tag_open'] = '<li>';
			$config['last_tag_close'] = '</li>';
			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';
			$config['full_tag_open'] = '<div class="pagging"><ul>';
			$config['full_tag_close'] = ' </ul></div>';

			$config['cateid'] =  $this->msystem->get_global('cat_id');
			$config['cur_page'] = $this->msystem->get_global('cur_page');
			$config['base_url'] = $this->msystem->get_global('base_url');
			$config["per_page"] = $config['limit'];
			$config['total_rows'] = $this->post->num($params);
			$params['offset'] = ($config["cur_page"]-1)*$config["per_page"];
			
			$this->pagination->initialize($config);
		}

		$posts = $this->post->get_deploy_posts($params);
		if($posts)
		{
			foreach($posts as $i=>$post)
			{
				$config['posts'][$i] = $this->post->process_post_item($post);
				$config['posts'][$i]->image = image($post->post_thumb,'post_thumb');
				$config['posts'][$i]->image_large = image($post->post_thumb,'thumb_large');
				$config['posts'][$i]->alias = str_replace('_', ' ', $post->post_slug);
			}
			$this->view($config['view'], $config);
		}
	}
}